package com.shioirikukuri.mapper;

import com.shioirikukuri.entity.Order;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface OrderMapper {

	/**
	 * 插入一个订单记录
	 *
	 * @param order 订单对象
	 */
	@Insert({
			"<script>",
			"INSERT INTO `order` (",
			"order_id, phone_number, title, service_total_price, food_total_price, converted_amount, status, order_time, payment_time, expired_time, refunded_time",
			") VALUES (",
			"#{orderId}, #{phoneNumber}, #{title}, #{serviceTotalPrice}, #{foodTotalPrice}, #{convertedAmount}, #{status}, #{orderTime}, #{paymentTime}, #{expiredTime}, #{refundedTime}",
			")",
			"</script>"
	})
	void insertOrder(Order order);

	/**
	 * 根据订单ID查询订单
	 *
	 * @param orderId 订单ID
	 * @return 订单对象
	 */
	@Select("SELECT * FROM `order` WHERE order_id = #{orderId}")
	Order selectById(String orderId);

	/**
	 * 更新订单状态
	 *
	 * @param orderId 订单ID
	 * @param status 新的状态
	 */
	@Update("UPDATE `order` SET status = #{status} WHERE order_id = #{orderId}")
	void updateStatusById(String orderId, String status);

	/**
	 * 更新支付时间并修改状态为已支付
	 *
	 * @param orderId 订单ID
	 * @param paymentTime 支付时间
	 */
	@Update("UPDATE `order` SET status = #{status}, payment_time = #{paymentTime} WHERE order_id = #{orderId}")
	void updatePaymentTimeAndStatus(String orderId, String paymentTime, String status);

	/**
	 * 更新退款时间并修改状态为已退款
	 *
	 * @param orderId 订单ID
	 * @param refundedTime 退款时间
	 */
	@Update("UPDATE `order` SET status = #{status}, refunded_time = #{refundedTime} WHERE order_id = #{orderId}")
	void updateRefundedTimeAndStatus(String orderId, String refundedTime, String status);
}